import _defineProperty from "@babel/runtime/helpers/esm/defineProperty";
import _objectWithoutProperties from "@babel/runtime/helpers/esm/objectWithoutProperties";
import _classCallCheck from "@babel/runtime/helpers/esm/classCallCheck";
import _createClass from "@babel/runtime/helpers/esm/createClass";
import _inherits from "@babel/runtime/helpers/esm/inherits";
import _createSuper from "@babel/runtime/helpers/esm/createSuper";
import _applyDecoratedDescriptor from "@babel/runtime/helpers/esm/applyDecoratedDescriptor";
import _isEmpty from "lodash/isEmpty";
var _excluded = ["fetchingNotices", "locale"];

var _dec, _dec2, _class, _class2, _class3;

import React, { PureComponent } from 'react';
import { Popover, Icon, Badge, Spin } from 'antd';
import { connect } from 'dva';
import { Bind } from 'lodash-decorators';
import monent from 'moment';
import classNames from 'classnames';
import { routerRedux } from 'dva/router'; // import SockJs from 'sockjs-client';
// import Stomp from 'webstomp-client';
// import messageIcon from '../../assets/notices.png';

import intl from "../../utils/intl";
import emptyMessageIcon from "../../assets/empty-message.png"; // import bellIcon from '../../assets/userMessageIcon.pic';

import List from "./NoticeList";
import styles from "./index.less?modules";
var noCountOffset = [-2, 0];
var countLt10Offset = [-2, 4];
var countGt10Offset = [-2, 6];
var NoticeIcon = (_dec = connect(function (_ref) {
  var _ref$global = _ref.global,
      global = _ref$global === void 0 ? {} : _ref$global,
      loading = _ref.loading;
  return {
    count: global.count,
    notices: global.notices,
    fetchingNotices: loading.effects['global/fetchNotices']
  };
}), _dec2 = Bind(), _dec(_class = (_class2 = (_class3 = /*#__PURE__*/function (_PureComponent) {
  _inherits(NoticeIcon, _PureComponent);

  var _super = _createSuper(NoticeIcon);

  function NoticeIcon(props) {
    var _this;

    _classCallCheck(this, NoticeIcon);

    _this = _super.call(this, props);
    _this.state = {
      visible: false
    };

    _this.handleItemClick = function (item) {
      var dispatch = _this.props.dispatch;
      dispatch(routerRedux.push({
        pathname: "/cmsg/user-message/detail/".concat(item.id)
      }));

      _this.setState({
        visible: false
      });
    };

    _this.handleNoticeVisibleChange = function (visible) {
      _this.setState({
        visible: visible
      });

      if (visible) {
        _this.props.dispatch({
          type: 'global/fetchNotices'
        });
      }
    };

    _this.onTabChange = function (tabType) {
      _this.setState({
        tabType: tabType
      });

      _this.props.onTabChange(tabType);
    };

    var title = props.title;

    if (title) {
      _this.state.tabType = title;
    }

    return _this;
  }
  /**
   * 点击查看更多跳转页面
   */


  _createClass(NoticeIcon, [{
    key: "handleNoticeList",
    value: function handleNoticeList(e) {
      e.preventDefault();
      var dispatch = this.props.dispatch;
      dispatch(routerRedux.push({
        pathname: "/cmsg/user-message/list"
      }));
      this.setState({
        visible: false
      });
    }
    /**
     * 点击每一行跳转到详情界面
     */

  }, {
    key: "getNotificationBox",
    value: function getNotificationBox() {
      var _this2 = this;

      var _this$props = this.props,
          loading = _this$props.fetchingNotices,
          locale = _this$props.locale,
          childProps = _objectWithoutProperties(_this$props, _excluded);

      if (_isEmpty(childProps)) {
        return null;
      }

      var notices = childProps.notices,
          contentTitle = childProps.contentTitle,
          emptyText = childProps.emptyText,
          emptyImage = childProps.emptyImage,
          title = childProps.title;
      var newNotices = notices && notices.map(function (item) {
        return {
          id: item.messageId,
          title: item.subject,
          datetime: monent(item.creationDate).fromNow() // avatar: messageIcon,

        };
      });
      var panes = /*#__PURE__*/React.createElement(List, {
        data: newNotices,
        onClick: this.handleItemClick,
        onClear: function onClear() {
          return _this2.props.onClear(title);
        },
        onMore: this.handleNoticeList,
        locale: locale // 以下为item的内容
        ,
        title: title,
        emptyText: emptyText,
        emptyImage: emptyImage,
        contentTitle: contentTitle,
        contentTitleAction: /*#__PURE__*/React.createElement("a", {
          onClick: this.handleNoticeList
        }, intl.get('cpaas.common.basicLayout.gotoUserMessage').d('进入消息中心'))
      });
      return /*#__PURE__*/React.createElement(Spin, {
        spinning: loading,
        delay: 0
      }, panes);
    }
  }, {
    key: "render",
    value: function render() {
      var _classNames;

      var _this$props2 = this.props,
          className = _this$props2.className,
          count = _this$props2.count,
          popupAlign = _this$props2.popupAlign;
      var noticeButtonClass = classNames((_classNames = {}, _defineProperty(_classNames, className, true), _defineProperty(_classNames, styles.noticeButton, true), _defineProperty(_classNames, styles.hasCount, count > 0), _classNames));
      var offset = noCountOffset;

      if (count > 9) {
        offset = countGt10Offset;
      } else if (count > 0) {
        offset = countLt10Offset;
      }

      var notificationBox = this.getNotificationBox();
      var visible = this.state.visible;
      var bellIconClass = visible ? "".concat(styles['icon-active'], " ").concat(styles.icon) : styles.icon;
      var trigger = /*#__PURE__*/React.createElement("span", {
        className: noticeButtonClass
      }, /*#__PURE__*/React.createElement(Badge, {
        count: count,
        className: styles.badge,
        offset: offset
      }, /*#__PURE__*/React.createElement(Icon, {
        className: bellIconClass
      })));

      if (!notificationBox) {
        return trigger;
      }

      return /*#__PURE__*/React.createElement(Popover, {
        placement: "bottomRight",
        content: notificationBox,
        popupClassName: styles.popover,
        trigger: "click",
        arrowPointAtCenter: true,
        popupAlign: popupAlign,
        onVisibleChange: this.handleNoticeVisibleChange,
        visible: visible
      }, trigger);
    }
  }]);

  return NoticeIcon;
}(PureComponent), _class3.defaultProps = {
  onTabChange: function onTabChange() {},
  onClear: function onClear() {},
  locale: {
    emptyText: intl.get('cpaas.common.components.noticeIcon.null').d('暂无数据'),
    clear: intl.get('cpaas.common.button.clear').d('清空')
  },
  emptyImage: emptyMessageIcon
}, _class3), (_applyDecoratedDescriptor(_class2.prototype, "handleNoticeList", [_dec2], Object.getOwnPropertyDescriptor(_class2.prototype, "handleNoticeList"), _class2.prototype)), _class2)) || _class);
export { NoticeIcon as default };